<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <div class="pub-table-action-btn">
          <a-button
            v-btnPermission="'mm_025_add_01'"
            type="primary"
            icon="plus"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>

          <excel
            ref="exportSelect"
            file-name="物资检查记录"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportSelect"
            >
              导出选中
            </a-button>
          </excel>
          <excel
            ref="exportAll"
            file-name="物资检查记录"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportAll"
            >
              导出全部
            </a-button>
          </excel>
        </div>
        <div class="pub-table-select-count">
          <a-icon
            type="info-circle"
            theme="twoTone"
          />
          已选择{{ selectedRowKeys.length }}项
          <span style="padding-left: 10px;">
            总计：{{ totalSize }}，
            草稿：{{ statistics.draft }}，
            已提交：{{ statistics.past }}
          </span>
        </div>
      </div> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :exportConfig="{ use: false, select: '导出选中', all: '导出全部' }"
        :exportPermission="{ select: '', all: '' }"
        :is-show-delete-count="false"
      >
        <a-button
          slot="prefix"
          v-btnPermission="'mm_025_add_01'"
          type="primary"
          icon="plus"
          @click="handleEdit(null, 1)"
        > 新增 </a-button>
        <template
          slot="index"
          slot-scope="text, record, index"
        >
          {{ index + 1 }}
        </template>
        <template
          slot="auditStatusStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.auditStatus"
            :text="text"
          />
        </template>
        <div
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a @click="handleEdit(record, 3)">查看</a>
            <a
              v-if="record.auditStatus === 'PAST'&&hideBuquBtn()"
              @click="invalidResult(record)"
            >作废</a>
            <template v-if="(record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED')&&hideBuquBtn()">
              <a @click="() => handleEdit(record, 2)">编辑</a>
              <a @click="() => handleDel(record.id)">删除</a>
            </template>
          </MoreButton>
        </div>
      </list-table>
      <!-- <pagination
        v-model="pageNo"
        :total="totalSize"
        :page-size.sync="pageSize"
        @change="handlePageChange"
        @showSizeChange="showSizeChange"
      /> -->
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { recordList, recordListDel, invalid, findListCount } from '@/api/material/check/record'
import { selectOption } from '@/api/material/goods/goods'

export default {
  name: 'a' + Date.now(),
  data: function () {
    return {
      loading: false,
      queryParam: {},
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      searchArr: [
        {
          name: '物资检查记录编码',
          value: 'code',
          type: 'input'
        },
        {
          name: '检查单位',
          value: 'checkOrgName',
          type: 'input'
        },
        {
          name: '受检查单位',
          value: 'inspectedUnitName',
          type: 'input'
        },
        {
          name: '检查地点',
          value: 'checkPlaceId',
          type: 'select',
          data: []
        },
        {
          name: '检查时间',
          value: 'checkDate',
          type: 'dateRange'
        },
        {
          name: '编号',
          value: 'number',
          type: 'input'
        },
        {
          name: '综合评价结论',
          value: 'comEvalConclusionId',
          type: 'select',
          data: []
        },
        {
          name: '状态',
          value: 'auditStatus',
          type: 'select',
          data: [
            { label: '草稿', value: 'DRAFT', enabled: true },
            { label: '已提交', value: 'PAST', enabled: true },
            { label: '已作废', value: 'DELETE', enabled: true }
          ]
        }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'index' },
          width: 70
        },
        {
          title: '物资检查记录编码',
          dataIndex: 'code',
          width: 140
        },
        {
          title: '检查地点',
          dataIndex: 'checkPlaceStr',
          width: 120
        },
        {
          title: '检查单位',
          dataIndex: 'checkOrgName',
          width: 120
        },
        {
          title: '受检查单位',
          dataIndex: 'inspectedUnitName',
          width: 120
        },
        {
          title: '检查时间',
          dataIndex: 'checkDate',
          width: 120
        },
        {
          title: '编号',
          dataIndex: 'number',
          width: 120
        },
        {
          title: '综合评价结论',
          dataIndex: 'comEvalConclusionStr',
          width: 120
        },
        {
          title: '状态',
          width: this.STATUS_WIDTH,
          dataIndex: 'auditStatusStr',
          scopedSlots: { customRender: 'auditStatusStr' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 150,
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      selectedRowKeys: [],
      statistics: {},
      selectId: [],
      selectedRows: [],
      chooseData: 0,
      tableData: [],
      loadData: (params) => {
        let searchData = Object.assign(params, this.queryParam)
        if (searchData.checkDate?.length > 0) {
          delete searchData.checkDateStart
          delete searchData.checkDateEnd
        }
        if (searchData?.checkDate) {
          searchData.checkDateStart = this.formatDate(searchData.checkDate[0])
          searchData.checkDateEnd = this.formatDate(searchData.checkDate[1])
        }
        findListCount(searchData).then((res) => {
          if (res.code === this.SUCCESS_CODE) this.statistics = res.data
        })
        return recordList(searchData).then((res) => {
          return res
        })
      },
      pathName: this.$route.path
    }
  },
  created() {
    // this.list()
  },
  mounted: function () {
    // this.list()
    this.$refs.table.refresh()
    this.getAddress()
    this.getConclusion()
    // this.$refs.exportSelect.tableToExcelFile(this.$refs.table)
    // this.$refs.exportAll.tableToExcelFile(this.$refs.table)
    console.log('mounted-----------table的Path', this.pathName)
    // 监听详情页返回刷新列表
    //this.pathName取的值是this.$route.path
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      // console.log('--------------------收到了，刷新刷新刷新----------------', this.pathName)
      // this.list()
      this.$refs.table.refresh()
    })
  },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    exportSelect: function () {
      if (this.selectedRowKeys.length > 0) {
        this.$refs.exportSelect.setDataAndGenerate(this.selectedRows)
      } else {
        this.$message.warning('请选择需要导出的数据')
      }
    },
    exportAll: function () {
      // 参数：查询条件、导出的回调
      if (this.totalSize) {
        this.pageNo = 1
        this.list(false, this.$refs.exportAll.setDataAndGenerate)
      } else {
        this.$message.warning('没有可导出的数据')
      }
    },
    invalidResult: function (row) {
      this.invalidList(invalid, this.search, { id: row.id })
    },
    getAddress: function () {
      let _this = this
      selectOption({
        type: 30
      }).then((res) => {
        if (res.code === 200) {
          for (let i = 0; i < res.data.length; i++) {
            _this.searchArr[3].data.push({ label: res.data[i].name, value: res.data[i].id })
          }
        }
      })
    },
    getConclusion: function () {
      let _this = this
      selectOption({
        type: 31
      }).then((res) => {
        if (res.code === 200) {
          for (let i = 0; i < res.data.length; i++) {
            _this.searchArr[6].data.push({ label: res.data[i].name, value: res.data[i].id })
          }
        }
      })
    },
    list(params, callback) {
      // this.loading = true
      // let _this = this
      // let searchData = Object.assign(
      //   {
      //     pageSize: callback ? _this.totalSize : _this.pageSize,
      //     pageNo: _this.pageNo
      //   },
      //   params,
      //   this.queryParam
      // )
      // if (searchData.checkDate) {
      //   searchData.checkDateStart = _this.formatDate(searchData.checkDate[0])
      //   searchData.checkDateEnd = _this.formatDate(searchData.checkDate[1])
      // }
      // findListCount(searchData).then((res) => {
      //   if (res.code === this.SUCCESS_CODE) this.statistics = res.data
      // })
      // recordList(searchData).then((res) => {
      //   if (res.code === 200) {
      //     if (callback) {
      //       if (res.data.length) {
      //         callback(res.data)
      //       } else {
      //         this.$message.warning('没有可导出的数据')
      //       }
      //       this.loading = false
      //       return
      //     }
      //     _this.tableData = res.data
      //     _this.totalSize = res.totalSize
      //   }
      //   this.loading = false
      // })
    },
    search(val) {
      this.pageNo = 1
      this.queryParam = val
      for (let prop in this.queryParam) {
        if(!this.queryParam[prop]) {
          delete this.queryParam[prop]
        }
      }
      // this.list(val)
      this.$refs.table.refresh()
    },
    // handlePageChange(pageNo, pageSize) {
    //   this.pageNo = pageNo
    //   this.list()
    //   this.selectedRowKeys = []
    // },
    // showSizeChange(pageNo, pageSize) {
    //   this.pageNo = 1
    //   this.pageSize = pageSize
    //   this.list()
    // },
    // onSelectChange(selectedRowKeys, selectedRows) {
    //   let _this = this
    //   _this.selectId = []
    //   selectedRows.map((item) => {
    //     _this.selectId.push(item.id)
    //   })
    //   this.chooseData = selectedRowKeys.length
    //   _this.selectedRowKeys = selectedRowKeys
    //   _this.selectedRows = selectedRows
    // },
    handleDel(id) {
      let _this = this
      let params = id ? [id] : _this.selectId
      _this.delList(
        recordListDel,
        () => {
          _this.search()
          this.selectedId = []
          this.selectedRowKeys = []
          this.selectedRows = []
          this.editingKey = ''
        },
        { ids: params }
      )
    },
    handleEdit(record, state) {
      this.$router.push({
        path: '/material/check/record/add',
        query: {
          id: record && record.id,
          state: state
        }
      })
    }
  }
}
</script>
